package com.sky.demo.activities;

import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.sky.demo.R;
import com.sky.demo.adapter.RecyclerAdapter;
import com.sky.demo.adapter.WaterFallAdapter;
import com.sky.demo.global.BaseActivity;
import com.sky.demo.recyclerview.itemanimator.ScaleInOutItemAnimator;
import com.sky.demo.recyclerview.itemanimator.SlideInOutBottomItemAnimator;
import com.sky.demo.recyclerview.itemanimator.SlideInOutLeftItemAnimator;
import com.sky.demo.recyclerview.itemanimator.SlideInOutRightItemAnimator;
import com.sky.demo.recyclerview.itemanimator.SlideInOutTopItemAnimator;
import com.sky.demo.recyclerview.itemanimator.SlideScaleInOutRightItemAnimator;
import com.sky.demo.recyclerview.itemdecoration.DividerGridItemDecoration;

import java.util.ArrayList;
import java.util.List;

/**
 * @author SKY
 * @Description: 瀑布流
 * @date 2015/7/31 11:27
 */
public class WaterFallFlowActivity extends BaseActivity {
    private RecyclerView recyclerView;
    private List<String> mDatas;
    private WaterFallAdapter mAdater;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler);
        initDatas();
        initViews();
    }

    private void initViews() {
        recyclerView = (RecyclerView) findViewById(R.id.recycle);
        mAdater = new WaterFallAdapter(WaterFallFlowActivity.this, mDatas);
        recyclerView.setAdapter(mAdater);
        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(5, StaggeredGridLayoutManager.VERTICAL));
        recyclerView.addItemDecoration(new DividerGridItemDecoration(this));

//设置分割线
//        recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        mAdater.setOnItemClickListener(new RecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
//                showToast("onItemClick="+position);
                mAdater.addItem(mDatas.get((int) (Math.random() * mDatas.size())), position);
                mAdater.addItem(mAdater.getmDatas().get(position), position);
//                mDatas.get((int) (Math.random() * mDatas.size()))

            }

            @Override
            public void onItemLongClick(View view, int position) {
                showToast("onItemLongClick=" + position);
                mAdater.deleteItem(position);
            }
        });
    }

    private void initDatas() {
        mDatas = new ArrayList<>();
        for (int i = 'A'; i <= 'z'; i++) {
            mDatas.add((char) i + "");
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.recycle_stagger_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_vertical:
                recyclerView.setLayoutManager(new StaggeredGridLayoutManager(5, StaggeredGridLayoutManager.VERTICAL));
                break;
            case R.id.action_horizontal:
                recyclerView.setLayoutManager(new StaggeredGridLayoutManager(5, StaggeredGridLayoutManager.HORIZONTAL));
                break;
            case R.id.action_add:
                if (mDatas.size() != 0)
                    mAdater.addItem(mDatas.get((int) (Math.random() * mDatas.size())), 1);
                break;
            case R.id.action_delete:
                mAdater.deleteItem(1);
                break;
            case R.id.action_left:
                recyclerView.setItemAnimator(new SlideInOutLeftItemAnimator(recyclerView));
                break;
            case R.id.action_right:
                recyclerView.setItemAnimator(new SlideInOutRightItemAnimator(recyclerView));
                break;
            case R.id.action_top:
                recyclerView.setItemAnimator(new SlideInOutTopItemAnimator(recyclerView));
                break;
            case R.id.action_bottom:
                recyclerView.setItemAnimator(new SlideInOutBottomItemAnimator(recyclerView));
                break;
            case R.id.action_scale:
                recyclerView.setItemAnimator(new ScaleInOutItemAnimator(recyclerView));
                break;
            case R.id.action_scaleright:
                recyclerView.setItemAnimator(new SlideScaleInOutRightItemAnimator(recyclerView));
                break;

        }
        return super.onOptionsItemSelected(item);
    }
}
